﻿<%@ WebHandler Language="C#" Class="SIPO.Web.Ajaxhandler.Administration.UserManagement.sParameterhandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web;

using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration.UserManagement;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using System.Globalization;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;

namespace SIPO.Web.Ajaxhandler.Administration.UserManagement
{

    public class sParameterhandler : BaseHandler
    {
        private recParameterRepo ParameterDal = new recParameterRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

        string MethodName = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            if (context.Request.Params["method"] != null)
                MethodName = context.Request.Params["method"];

            if (MethodName == "update")
            {
                context.Response.Clear();
                context.Response.Buffer = true;

                System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);

                string jsonString = string.Empty;
                jsonString = context.Server.UrlDecode(sr.ReadToEnd());
                JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

                sr.Close();
                sr.Dispose();

                JSONHelper helper = new JSONHelper();

                try
                {
                    recParameter obj = new recParameter();

                    if (jObj["Value"].ToString().Trim().Length > 1)
                    {
                        DateTime serverDate = Helper.FormatDate(jObj["Value"].ToString(), Helper.Culture.Indonesian);
                        obj.Value = serverDate.ToString("yyyy-MM-dd");
                    }
                    else {
                        obj.Value = null;
                    }
                        
                    
                    obj.ParameterCode = jObj["ParameterCode"].ToString();
                    obj.Remark = "Tanggal Server";
                    obj.UpdatedDate = DateTime.Now;
                    obj.UpdatedBy = base.UserLoggedIn;
                    ParameterDal.Update(obj);

                    
                    helper.Message = "Data berhasil di ubah !";
                    helper.IsSuccess = true;
                }
                catch (Exception ex)
                {
                    helper.IsSuccess = false;
                    helper.Message = ex.Message;
                }
                finally
                {
                    recLog log = new recLog();
                    log.LogEvent = "Update";
                    log.LogMessage = helper.Message;
                    log.LogSource = "recParameterhandler.ashx";
                    log.CreatedBy = base.UserLoggedIn;
                    log.CreatedDate = DateTime.Now;
                    logDal.Save(log);
                }

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(helper));
                                
            }
        }
        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}